package com.millstein.tsinglog.controller;

import com.millstein.tsinglog.annotation.ResponseResult;
import com.millstein.tsinglog.annotation.ShiroAccessLog;
import com.millstein.tsinglog.commons.constant.SystemConstant;
import com.millstein.tsinglog.model.dto.comment.BatchCommentCheckDTO;
import com.millstein.tsinglog.model.dto.comment.CommentCheckDTO;
import com.millstein.tsinglog.model.vo.biz.CommentVo;
import com.millstein.tsinglog.model.vo.result.PageResult;
import com.millstein.tsinglog.result.ProfileResult;
import com.millstein.tsinglog.service.CommentService;
import com.millstein.tsinglog.service.MailService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/comment")
@ResponseResult
@CrossOrigin
public class CommentController {

    @Autowired
    private CommentService commentService;


    /**
     * 条件及分页查询评论信息
     * @param currentPage 当前页
     * @param pageSize 每页的数据量
     * @param ip 评论者的ip地址
     * @param keyword 关键字
     * @param commentStatus 评论的审核状态
     * @return
     */
    @GetMapping("/getCommentByPage")
    @RequiresPermissions("comment:query")
    @ShiroAccessLog("查询评论信息")
    public PageResult<CommentVo> getCommentByPage(Integer currentPage,
                                                  Integer pageSize,
                                                  String ip,
                                                  String keyword,
                                                  Short commentStatus) {
        return commentService.getCommentByPage(currentPage, pageSize, ip, keyword, commentStatus);
    }

    /**
     * 分页查询前五条未审核的评论信息
     * @return
     */
    @GetMapping("/getCommentForUnchecked")
    @RequiresPermissions("comment:check")
    public PageResult<CommentVo> getCommentForUnchecked() {
        // 固定只查第一页
        return commentService.getCommentByPage(SystemConstant.DEFAULT_CURRENT_PAGE,
                SystemConstant.PAGE_SIZE_FOR_UNCHECKED_COMMENT, null, null,
                SystemConstant.COMMENT_HAS_NOT_BEEN_AUDITED);
    }

    /**
     * 审核评论的方法
     * @param commentCheckDTO 评论的结果信息
     * @return
     */
    @GetMapping("/checkComment")
    @RequiresPermissions("comment:check")
    @ShiroAccessLog("审核评论")
    public Short checkComment(@Valid CommentCheckDTO commentCheckDTO) {
        // 1.获取安全数据
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        ProfileResult result = (ProfileResult) principals.getPrimaryPrincipal();
        // 2.审核评论
        commentService.checkComment(commentCheckDTO, result.getId());
        return commentCheckDTO.getCheckResult();
    }

    /**
     * 根据id查询评论
     * @param id 评论的id
     * @return
     */
    @GetMapping("/getCommentById")
    @RequiresPermissions("comment:check")
    public CommentVo getCommentById(Long id) {
        return commentService.getCommentById(id);
    }

    /**
     * 修改审核信息
     * @param commentCheckDTO 审核的信息
     * @return
     */
    @GetMapping("/modifyCheck")
    @RequiresPermissions("comment:check")
    @ShiroAccessLog("修改评论信息")
    public Short modifyCheck(@Valid CommentCheckDTO commentCheckDTO) {
        // 1.获取安全数据
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        ProfileResult result = (ProfileResult) principals.getPrimaryPrincipal();
        commentService.modifyCheck(commentCheckDTO, result.getId());
        return commentCheckDTO.getCheckResult();
    }

    /**
     * 批量审核评论的方法
     * @param batchCommentCheckDTO 审核的信息
     * @return
     */
    @PostMapping("/batchCheckComment")
    @RequiresPermissions("comment:check")
    @ShiroAccessLog("批量审核评论")
    public void batchCheckComment(@RequestBody @Valid BatchCommentCheckDTO batchCommentCheckDTO) {
        // 1.获取安全数据
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        ProfileResult result = (ProfileResult) principals.getPrimaryPrincipal();
        commentService.batchCheckComment(batchCommentCheckDTO, result.getId());
    }

    /**
     * 批量删除评论，假删除
     * @param ids 评论ids的集合
     * @return
     */
    @GetMapping("/deleteByIds")
    @RequiresPermissions("comment:delete")
    @ShiroAccessLog("删除评论信息")
    public void deleteByIds(@RequestParam("ids") List<Long> ids) {
        commentService.deleteByIds(ids);
    }
}
